package com.apiserver.producer.system.controller;

import com.apiserver.kernel.utils.MapUtil;
import com.apiserver.model.system.entity.SUsers;
import com.apiserver.producer.system.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(description = "用户信息管理", tags = "UsersController", basePath = "/users")
@RestController
@RequestMapping(value = "/v1/users", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UsersController {

    @Autowired
    UsersService usersService;


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据用户ID，查询用户的详细信息", notes = "返回单个用户的信息,菜单详细信息")
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public SUsers get(@RequestParam(value = "id") Long id) {
        return usersService.selectById(id);
    }




    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据查询的参数，查询用户的列表信息", notes = "返回用户的列表信息")
    @RequestMapping(value = "getAll", method = RequestMethod.GET)
    public PageInfo<SUsers> getAll(@RequestParam(value = "pageNum") int pageNum, @RequestParam(value = "pageSize") int pageSize,
                                   @RequestParam(value = "account", required = false) String account, @RequestParam(value = "mobile", required = false) String mobile,
                                   @RequestParam(value = "email", required = false) String email, @RequestParam(value = "status", required = false) Integer status, @RequestParam(value = "loginType", required = false) Integer loginType) {


        PageHelper.startPage(pageNum, pageSize);
        List<String> likeParam = new ArrayList<>();
        likeParam.add("account");
        likeParam.add("email");
        likeParam.add("mobile");

        Map<String, Object> orderMap = new LinkedHashMap<>();
        orderMap.put("registerTime", "desc");
        List<SUsers> sUsersList = usersService.selectAllByOtherKey(SUsers.builder().account(account)
                .mobile(mobile)
                .email(email)
                .status(status)
                .loginType(loginType)
                .build(), null, null,null);

        PageInfo<SUsers> pageInfo = new PageInfo<>(sUsersList);
        return pageInfo;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存用户的信息", notes = "保存用户的信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Map<String, Object> save(@RequestBody SUsers usersEntity) {
        usersService.insert(usersEntity);
        return null;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存用户的信息", notes = "保存用户的信息")
    @RequestMapping(value = "edite", method = RequestMethod.POST)
    public Map<String, Object> edite(@RequestBody SUsers usersEntity) {

        Map<String, Object> result = new HashMap<>();

            usersService.updateById(usersEntity);

        return result;
    }





    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除用户的信息", notes = "根据用户的ID,删除数据")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public Map<String, Object> delete(@RequestBody Long id) {
        Map<String, Object> resultMap = new HashMap<>();
        usersService.deleteById(id) ;
        return resultMap;
    }




    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "系统用户唯一ID", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "account", value = "用户系统账号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "系统密码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "chineseName", value = "中文名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "nickname", value = "用户昵称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "email", value = "用户邮箱", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "手机号码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "lastDate", value = "最后登录时间", required = false, dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "sex", value = "性别", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "birthday", value = "出生年月", required = false, dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "nationality", value = "国籍", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "province", value = "省份", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "cities", value = "城市", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "居住地址", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "idType", value = "证件类型", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "idNumber", value = "证件号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "education", value = "学历", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "age", value = "年龄", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "household", value = "户籍地址", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ethnic", value = "民族", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "blood", value = "血型", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "career", value = "职业", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "registerTime", value = "注册时间", required = false, dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "激活状态", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "loginType", value = "登陆类型", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "skin", value = "用户皮肤", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "modifyTime", value = "修改时间", required = false, dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "modifyId", value = "修改人", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "imageUrl", value = "头像路径", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userType", value = "用户类型", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "registerType", value = "注册类型", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "registerCode", value = "注册码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "qqNumber", value = "qq账号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "wechat", value = "微信号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "weibo", value = "微博地址", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "openIdWechat", value = "微信的openID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "openIdQq", value = "qq注册生成id", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "openIdWeibo", value = "微博注册生成ID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "Authorization", value = "token", required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "列表", notes = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public List<SUsers> list(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "pwd", required = false) String pwd,
            @RequestParam(value = "chineseName", required = false) String chineseName,
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "mobile", required = false) String mobile,
            @RequestParam(value = "lastDate", required = false) Date lastDate,
            @RequestParam(value = "sex", required = false) String sex,
            @RequestParam(value = "birthday", required = false) Date birthday,
            @RequestParam(value = "nationality", required = false) String nationality,
            @RequestParam(value = "province", required = false) String province,
            @RequestParam(value = "cities", required = false) String cities,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(value = "idType", required = false) Integer idType,
            @RequestParam(value = "idNumber", required = false) String idNumber,
            @RequestParam(value = "education", required = false) String education,
            @RequestParam(value = "age", required = false) Integer age,
            @RequestParam(value = "household", required = false) String household,
            @RequestParam(value = "ethnic", required = false) String ethnic,
            @RequestParam(value = "blood", required = false) String blood,
            @RequestParam(value = "career", required = false) String career,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "loginType", required = false) Integer loginType,
            @RequestParam(value = "skin", required = false) String skin,
            @RequestParam(value = "modifyId", required = false) String modifyId,
            @RequestParam(value = "roleId", required = false) Long roleId,
            @RequestParam(value = "imageUrl", required = false) String imageUrl,
            @RequestParam(value = "userType", required = false) Integer userType,
            @RequestParam(value = "registerType", required = false) Integer registerType,
            @RequestParam(value = "registerCode", required = false) String registerCode,
            @RequestParam(value = "qqNumber", required = false) String qqNumber,
            @RequestParam(value = "wechat", required = false) String wechat,
            @RequestParam(value = "weibo", required = false) String weibo,
            @RequestParam(value = "openIdWechat", required = false) String openIdWechat,
            @RequestParam(value = "openIdQq", required = false) String openIdQq,
            @RequestParam(value = "openIdWeibo", required = false) String openIdWeibo
    ) {

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("id",id);
        map.put("account",account);
        map.put("pwd",pwd);
        map.put("chinese_name",chineseName);
        map.put("nickname",nickname);
        map.put("email",email);
        map.put("mobile",mobile);
        map.put("last_date",lastDate);
        map.put("sex",sex);
        map.put("birthday",birthday);
        map.put("nationality",nationality);
        map.put("province",province);
        map.put("cities",cities);
        map.put("address",address);
        map.put("id_type",idType);
        map.put("id_number",idNumber);
        map.put("education",education);
        map.put("age",age);
        map.put("household",household);
        map.put("ethnic",ethnic);
        map.put("blood",blood);
        map.put("career",career);
        map.put("status",status);
        map.put("login_type",loginType);
        map.put("skin",skin);
        map.put("modify_id",modifyId);
        map.put("role_id",roleId);
        map.put("image_url",imageUrl);
        map.put("user_type",userType);
        map.put("register_type",registerType);
        map.put("register_code",registerCode);
        map.put("qq_number",qqNumber);
        map.put("wechat",wechat);
        map.put("weibo",weibo);
        map.put("open_id_wechat",openIdWechat);
        map.put("open_id_qq",openIdQq);
        map.put("open_id_weibo",openIdWeibo);
        MapUtil.removeNullEntry(map);

        List<SUsers> list = usersService.selectAllNotGroup(map, null);
        return list;
    }
}
